package com.lizardmind.haofudoctor.ui.main

import android.app.ProgressDialog
import android.databinding.ObservableField
import android.view.View
import com.blankj.utilcode.util.RegexUtils
import com.linxiao.framework.rx.RxJavaUtil
import com.linxiao.framework.viewModel.LifecycleViewModel
import com.lizardmind.haofudoctor.AppApplication
import com.lizardmind.haofudoctor.manager.postApi
import com.lizardmind.haofudoctor.other.DoctorBean
import com.lizardmind.haofudoctor.other.DoctorInfo
import com.lizardmind.haofudoctor.other.Request
import com.lizardmind.haofudoctor.other.checkPassWord
import com.lizardmind.haofudoctor.test.TestActivity
import io.reactivex.functions.Action
import io.reactivex.functions.Consumer
import org.jetbrains.anko.intentFor
import org.jetbrains.anko.toast

/**
 *
 * Created by Extends on 2017/10/20 16:28
 */
class LoginVM :LifecycleViewModel() {
    var type : ObservableField<Int> = ObservableField(0)
    var phone : ObservableField<String> = ObservableField("")
    var password : ObservableField<String> = ObservableField("")
    var auth:ObservableField<String> = ObservableField("")
    var time: ObservableField<Int> = ObservableField(0)

    /**
     *  切换登陆类型
     */
    fun switchType(view: View) = type.set((type.get()+1)%2)

    /**
     * 获取验证码
     */
    fun getAuthCode(view:View){
        if(RegexUtils.isMobileExact(phone.get())){
            activity.get()?.apply {
                observe {
                    postApi<Request<String>>(hashMapOf("action" to "send_sms",
                            "mobile" to phone.get(),"type" to "1")
                            , Consumer {
                        it.dispose {
                            toast("发送成功")
                            RxJavaUtil.countDown(60).subscribe {
                                time.set(it)
                            }
                        }
                    })
                }
            }
        }else{
            activity.get()?.toast("手机号码不正确")
        }
    }

    /**
     * 登陆账号
     */
    fun login(view:View){
        activity.get()?.apply {
            if(type.get()==1||checkPassWord(password.get())){
                val progressDialog = ProgressDialog(this)
                progressDialog.setMessage("正在登陆")
                progressDialog.show()
                observe{
                    postApi<Request<DoctorBean>>(hashMapOf("action" to "doctor_register",
                            "type" to (type.get()+1).toString(),
                            "username" to phone.get(),
                            "password" to password.get(),
                            "userphone" to phone.get(),
                            "verify" to auth.get()),
                            Consumer {
                                it.dispose {
                                    DoctorInfo.updataDoctor(it)
                                    AppApplication.connect(it.rToken)
//                                    toast("登陆成功")
//                                    startActivity(intentFor<MainActivity>())
                                }
                            }, Action {
                        progressDialog.dismiss()
                    }
                    )
                }
            }else{
                toast("密码格式为数字、字母组成")
            }

        }
    }
}